package com.library.uiframework.widget.state;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.library.uiframework.R;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-09-23 18:03
 * @version: 9.1.0
 */
public class Loading extends ImageView {
    private Animation mAnimation;

    public Loading(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public Loading(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.unlimited_rotate);
        setAnimation(mAnimation);
    }

    public void startAnimation() {
        startAnimation(mAnimation);
    }

    public void stopAnimation() {
        clearAnimation();
    }

    public Loading(Context context) {
        super(context);
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if(visibility != GONE && visibility != INVISIBLE) {
            this.startAnimation();
        } else {
            this.stopAnimation();
        }

    }
}
